﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using HTES.TAS.Plugin;
using HTES.TAS.Engine.Extend;
using System.IO;

namespace RS
{
    public partial class RSAgricutureDroughtWaterloggingUI : UserControl
    {
        private ArrayList mInputImg1PathVec = new ArrayList();
        private ArrayList mInputImg2PathVec = new ArrayList();
        private ArrayList mOutputImgPathVec = new ArrayList();

        public RSAgricutureDroughtWaterloggingUI()
        {
            InitializeComponent();
        }

        public IPlugin Plugin
        {
            get;
            internal set;
        }

        //设置输入影像1路径
        public void setInputImg1PathVec(ArrayList inputImg1PathVec)
        {
            mInputImg1PathVec = null;
            mInputImg1PathVec = inputImg1PathVec;
        }

        //获取输入影像1路径
        public String inputImg1Path()
        {
            if (mInputImg1PathVec.Count < mComboBoxInputImg1.SelectedIndex)
            {
                return "";
            }
            return mInputImg1PathVec[mComboBoxInputImg1.SelectedIndex].ToString();
        }

        //设置输入影像2路径 
        public void setInputImg2PathVec(ArrayList inputImg2PathVec)
        {
            mInputImg2PathVec = null;
            mInputImg2PathVec = inputImg2PathVec;
        }

        //获取输入影像2路径 
        public String inputImg2Path()
        {
            if (mInputImg2PathVec.Count < mComboBoxInputImg2.SelectedIndex)
            {
                return "";
            }
            return mInputImg2PathVec[mComboBoxInputImg2.SelectedIndex].ToString();
        }

        //获取输出影像路径
        public String outputImgPath()
        {
            if (mOutputImgPathVec.Count < mComboBoxOutputImg.SelectedIndex)
            {
                return "";
            }
            return mOutputImgPathVec[mComboBoxOutputImg.SelectedIndex].ToString();
        }

        //获取算法类别，0表示干旱，1表示水灾
        public int flag()
        {
            return mComboBoxType.SelectedIndex;
        }

        //验证参数是否合法
        public bool VerifyArguments()
        {
            //验证第一张输入影像
            if (null == mComboBoxInputImg1.Text || !File.Exists(mComboBoxInputImg1.Text))
            {
                mLabelControlMsg.Text = "Input Image1 Path is invalid";
                return false;
            }

            //验证第二张输入影像
            if (null == mComboBoxInputImg2.Text || !File.Exists(mComboBoxInputImg2.Text))
            {
                mLabelControlMsg.Text = "Input Image2 Path is invalid";
                return false;
            }

            //验证输出影像
            if (null == mComboBoxOutputImg.Text)
            {
                mLabelControlMsg.Text = "Output Image Path is NULL";
                return false;
            }

            //验证输出影像是否为文件夹
            FileInfo fileInfo = new FileInfo(mComboBoxOutputImg.Text);
            if (fileInfo.Attributes == FileAttributes.Directory)
            {
                mLabelControlMsg.Text = "Output Image Path is Directory";
                return false;
            }

            return true;
        }

        //输入影像1浏览响应方法
        private void mBtnInputImg1Skim_Click(object sender, EventArgs e)
        {
            //定义打开文件对话框
            OpenFileDialog dlg = new OpenFileDialog();

            //设置对话框属性
            dlg.Filter = "tif jpg(*.tif *.jpg)|*.tif;*.jpg|tif(*.tif)|*.tif|All files (*.*)|*.*";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;

            //显示对话框
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                mInputImg1PathVec.Add(dlg.FileName);
                invalidComboBoxInputImg1();
            }
        }

        //输入影像2浏览响应方法
        private void mBtnInputImg2Skim_Click(object sender, EventArgs e)
        {
            //定义打开文件对话框
            OpenFileDialog dlg = new OpenFileDialog();

            //设置对话框属性
            dlg.Filter = "tif jpg(*.tif *.jpg)|*.tif;*.jpg|tif(*.tif)|*.tif|All files (*.*)|*.*";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;

            //显示对话框
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                mInputImg2PathVec.Add(dlg.FileName);
                invalidComboBoxInputImg2();
            }
        }

        //输出影像浏览响应方法
        private void mBtnOutputImgSkim_Click(object sender, EventArgs e)
        {
            //定义保存文件对话框
            SaveFileDialog dlg = new SaveFileDialog();

            //设置保存文件对话框属性
            dlg.Filter = "jpg(*.jpg)|*.jpg|tif(*.tif)|*.tif|All files (*.*)|*.*";
            dlg.FilterIndex = 2;

            //显示对话框
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                mOutputImgPathVec.Add(dlg.FileName);
                invalidComboBoxOutputImg();
            }
        }

        //刷新输入影像1组合框 
        private void invalidComboBoxInputImg1()
        {
            mComboBoxInputImg1.Items.Clear();

            for (int i = 0; i < mInputImg1PathVec.Count; i++)
            {
                mComboBoxInputImg1.Items.Add(mInputImg1PathVec[i]);
            }

            mComboBoxInputImg1.SelectedIndex = 0;
        }

        //刷新输入影像2组合框 
        private void invalidComboBoxInputImg2()
        {
            mComboBoxInputImg2.Items.Clear();

            for (int i = 0; i < mInputImg2PathVec.Count; i++)
            {
                mComboBoxInputImg2.Items.Add(mInputImg2PathVec[i]);
            }

            mComboBoxInputImg2.SelectedIndex = 0;
        }

        //刷新输出影像组合框 
        private void invalidComboBoxOutputImg()
        {
            mComboBoxOutputImg.Items.Clear();

            for (int i = 0; i < mOutputImgPathVec.Count; i++)
            {
                mComboBoxOutputImg.Items.Add(mOutputImgPathVec[i]);
            }

            mComboBoxOutputImg.SelectedIndex = 0;
        }

        private void RSAgricutureDroughtWaterloggingUI_Load(object sender, EventArgs e)
        {
            mLabelControlMsg.Text = "";

            //获取TAS平台已经打开的遥感数据全路径列表
            var fileNames = this.Plugin.Host.MapControl.GetLayersFullPath();

            //加入字符串
            mInputImg1PathVec.AddRange(fileNames.ToArray());
            mInputImg2PathVec.AddRange(fileNames.ToArray());

            invalidComboBoxInputImg1();
            invalidComboBoxInputImg2();
        }
    }
}
